package com.leetcode.partition9;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/11/8 17:34
 */
public class LC875爱吃香蕉的珂珂 {

    public static int minEatingSpeed(int[] piles, int h) {
        //二分枚举答案，知道最小值和最大值
        int left = 1, right = Arrays.stream(piles).max().getAsInt();
        while (left < right) {
            int mid = (left + right) >>> 1;
            int needHour = 0;
            for (int pile : piles) {
                needHour += (pile - 1) / mid + 1;
            }
            if (needHour > h) {
                //吃的速度太慢，需要加快
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
}
